import { cloneDeep } from "lodash";
import { DataSourceTypeEnum, DataSourceTypeByDateEnum } from "@src/globalData/enum";
import { methodConfig } from "./config";
import { yunNao } from "@src/api/yunNao";
import {
  dataCategory,
  getDefaultDataBaseData,
  sendParamsStructure
} from "./constant";
import dayjs from "dayjs";
import { sendParamFilterData } from "./formatData";

/**调用云脑接口存储的数据结构 */
let sendParam: { [key: string]: any } = cloneDeep(sendParamsStructure);

/** 单个直接保存到Etrm数据库
 * @dataType  数据类型
 * @dataSource 数据源
 * @date  日期
 */
export const saveOneWithoutCal = async (
  dataType: DataSourceTypeEnum,
  dataSource: any,
  date: string
) => {
  let result = {};
  /**有爬取到值才进入数据转换，否则默认为空对象 */
  if (Object.keys(dataSource).length > 0) {
    result = await methodConfig[dataType].method(
      dataSource,
      date,
      methodConfig[dataType] as any
    );
  } else {
    result = {
      [dataType]: {},
    };
  }
  classifyData(result);
};

/**对爬取的数据进行分类，生成云脑接口入参格式数据
 * 然后计算每个分类请求数量是否一致，
 * 请求量数已达到就开始分类存储数据
 */
const classifyData = (formatData: any) => {
  const typeKey: any = Object.keys(formatData)[0];
  if (dataCategory[DataSourceTypeByDateEnum.D_REDUCE_DATE].includes(typeKey)) {
    sendParam[DataSourceTypeByDateEnum.D_REDUCE_DATE].data[0].data = Object.assign(
      sendParam[DataSourceTypeByDateEnum.D_REDUCE_DATE].data[0].data,
      { [typeKey]: formatData[typeKey] }
    );
    sendParam[DataSourceTypeByDateEnum.D_REDUCE_DATE].data[0].business_time = formatData.date;
    saveWebSpiderData(DataSourceTypeByDateEnum.D_REDUCE_DATE);
  } else if (dataCategory[DataSourceTypeByDateEnum.D_DATE].includes(typeKey)) {
    sendParam[DataSourceTypeByDateEnum.D_DATE].data[0].data = Object.assign(
      sendParam[DataSourceTypeByDateEnum.D_DATE].data[0].data,
      { [typeKey]: formatData[typeKey] }
    );
    sendParam[DataSourceTypeByDateEnum.D_DATE].data[0].business_time = formatData.date;
    saveWebSpiderData(DataSourceTypeByDateEnum.D_DATE);
  } else if (dataCategory.d_add_date.includes(typeKey)) {
    sendParam[DataSourceTypeByDateEnum.D_ADD_DATE].data[0].data = Object.assign(
      sendParam[DataSourceTypeByDateEnum.D_ADD_DATE].data[0].data,
      { [typeKey]: formatData[typeKey] }
    );
    sendParam[DataSourceTypeByDateEnum.D_ADD_DATE].data[0].business_time = formatData.date;
    saveWebSpiderData(DataSourceTypeByDateEnum.D_ADD_DATE);
  }
};


/**
 * 判断每个类型的数量是否达到
 * 调用云脑接口存储数据  通过云脑接口调用数据组数据，最终将数据存储到数组组数据库
 */
const saveWebSpiderData = async (type: string) => {
  const saveData = JSON.parse(JSON.stringify(sendParam[type]))
  
  /**置空已调用存储接口的类型数据，重新开始爬取存储 */
  console.log('存储原数据：', JSON.parse(JSON.stringify(sendParam[type].data)))
  sendParam[type].data = getDefaultDataBaseData()

  const {haveData, saveDataFilter} = sendParamFilterData(saveData.data[0].data)
  if(!haveData) {
    console.log('无数据，不存')
    return Promise.resolve()
  }
  saveData.data[0].data = saveDataFilter

    /**没有爬取时间的不往下执行 */
    if (!saveData.data[0].business_time) return;

    try {
      console.log('存储数据：', saveData)
      await yunNao.datapreservation([saveData]);
      console.log("获取数据存储成功", dayjs().format("YYYY-MM-DD HH:mm:ss"));
    } catch (error) {
      console.error("获取数据存储失败: ", error);
    }
}